<?php 
class azindia_register extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('register_model');
		//$this->load->library('form_validation');
		$this->load->library('session');
	}
	function index()
	{	
		$data['states'] = $this->register_model->getstates();
		//print_r($states);exit;
		
		$this->load->view('azindia_register_view', $data);
		
	}
	function index1()
	{
		redirect('login_ctrl');
	}
	function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
	function register()
	{	
	//print_r($_POST);exit;
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<span style="color:red" >','</span>');
		$this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|alpha');
		$this->form_validation->set_rules('email', 'Email', 'required|callback_checkemail');
		$this->form_validation->set_rules('password', 'Password', 'required|callback_password');
		$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|matches[password]');
		if($this->form_validation->run() == FALSE)
			{
		
		$data['register']= '';
		$this->load->view('azindia_register_view',$data);
		
			}else
			{
				//print_r($_POST);
				if(isset($_POST['allow_comments'])){
					$allow_comments = 'true';
				}else{
					$allow_comments = 'false';
					
				}
			$data = array('firstname'=>$this->input->post('first_name'),
							'lastname'=>$this->input->post('last_name'),
							'email'=>$this->input->post('email'),
							'password'=>$this->input->post('password'),
							'active'=>'false',
							'allowcomments'=>$allow_comments
						);
						
			//echo '<pre>';print_r($data);
			$string = http_build_query($data);
			$url = 'http://api2.azindia.com/api/user';
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST, true);
			curl_setopt($ch,CURLOPT_POSTFIELDS, $string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
			$response = json_decode(curl_exec($ch));
			curl_close($ch);
			//echo '<pre>';print_r($response);exit;
			if($response != ''){			
				$config = Array(
				 'protocol' => 'smtp',
				 'smtp_host' => 'ssl://smtp.googlemail.com.',
				 'smtp_port' => 465,
				 'smtp_user' => 'marrepallymahesh4@gmail.com', // change it to yours
				 'smtp_pass' => 'm8126109310b', // change it to yours
				 ); 
				 $message = "Please click below link to active your account\n".base_url()."index.php/azindia_register/active_account/".$response->id;
							//print_r($message);exit;
			  $this->load->library('email', $config);
			  $this->email->set_newline("\r\n");
			  $this->email->from('marrepallymahesh4@gmail.com', "Admin Team");
			  $this->email->to($this->input->post('email'));
			  $this->email->subject("Azindia Email Confirmation");
			  $this->email->message($message);
			  $this->email->send();
				$data['register'] = 'Thanks for registration. Please confirm your email';
				$this->load->view('azindia_register_view',$data);
				
			}else{
				$data['register']= 'Registration Failed';
				$this->load->view('azindia_register_view',$data);
			}
			}
	}
	
	function password($pass){
			if(!preg_match('/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/',$pass)){
				$this->form_validation->set_message('password_check','Password format is not correct');
				return false;
			}else{
				return true;
			}
	}
	
	function checkemail($email)
	{
		$result = $this->register_model->checkemail($email);
		//print_r($result);exit;
		if($result==0)
		{
			$this->form_validation->set_message('checkemail', 'Email is already exists');
			return false;
		}else
		{
			return true;
		}
	}
	
	function active_account(){
		$url = 'http://api2.azindia.com/api/user/'.$this->uri->segment(3);
		$data = '[{"op":"replace","path":"/active","value":"true"}]';
		$header = array('Content-Type: application/json');
		$ch = curl_init($url);
		curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PATCH');
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$response = json_decode(curl_exec($ch));
		curl_close($ch);
		redirect('login_ctrl');
	}
}
?>  
	